const fs = require("fs");
const path = require("path");
let file = path.resolve(__dirname, "./backup.txt");
let data = "jjjj";

function setData(txt) {
  fs.writeFile(file, txt, () => "");
}

function getData(callback) {
  fs.readFile(file, function (err, data) {
    console.log("异步读取: " + data);
    callback(data);
  });
}

const express = require("express");
const server = express();
server.use(express.json());
server.use(express.urlencoded({ extended: false }));
server.post("/setbackup", (req, res) => {
  setData(req.body.backup);
  res.header("Access-Control-Allow-Origin", "*");
  res.end("");
});
server.post("/getbackup", (req, res) => {
  res.header("Access-Control-Allow-Origin", "*");
  getData((data) => {
    res.end(data);
  });
});
server.listen(3000);
console.log("Server running at http://localhost:3000");
